package com.zrwl.poscloud.xo.service.impl;
import com.zrwl.poscloud.base.entity.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.enums.EBusiType;
import com.zrwl.poscloud.base.enums.EBusinessType;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.holder.RequestHolder;
import com.zrwl.poscloud.commons.utils.JsonUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.service.*;
import com.zrwl.poscloud.xo.vo.AdminVO;
import com.zrwl.poscloud.xo.vo.SequenceVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.zrwl.poscloud.xo.mapper.CallProductConfigMapper;
import com.zrwl.poscloud.xo.vo.CallProductConfigVO;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 通话产品配置 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-06-09
 */
@Service
public class CallProductConfigServiceImpl extends SuperServiceImpl<CallProductConfigMapper,CallProductConfig> implements CallProductConfigService {

    @Autowired
    private CallProductConfigService callProductConfigService;
    @Resource
    private CallProductConfigMapper callProductConfigMapper;
    @Autowired
    private AdminService adminService;
    @Autowired
    private SysDictDataService sysDictDataService;
    @Autowired
    private SequenceService sequenceService;
    @Override
    public IPage<CallProductConfigVO> getPageList(CallProductConfigVO callProductConfigVO){
       Page<CallProductConfig> page = new Page<>();
       page.setCurrent(callProductConfigVO.getCurrentPage());
       page.setSize(callProductConfigVO.getPageSize());
       IPage<CallProductConfigVO> pageList = callProductConfigMapper.getPageList(page,callProductConfigVO);
       List<CallProductConfigVO> list = pageList.getRecords();
       pageList.setRecords(list);
       return pageList;
}
    @Override
    public List<CallProductConfigVO> getAllList(CallProductConfigVO callProductConfigVO){
        List<CallProductConfigVO> list = callProductConfigMapper.getAllList(callProductConfigVO);
        List<SysDictData> phoneBillingWay = JsonUtils.jsonToList(JsonUtils.objectToJson(sysDictDataService.getListByDictType("phone_billing_way").get("list")), SysDictData.class);
        List<SysDictData> phoneService = JsonUtils.jsonToList(JsonUtils.objectToJson(sysDictDataService.getListByDictType("phone_service").get("list")), SysDictData.class);
        List<CallProductConfigVO> resultList = new ArrayList<>();
        for (CallProductConfigVO productConfigVO : list) {
            String configServiceName = null;
            if (phoneBillingWay != null) {
                for (SysDictData sysDictData : phoneBillingWay) {
                    if(productConfigVO.getBillingWay() == Integer.parseInt(sysDictData.getDictValue())){
                        productConfigVO.setBillingWayName(sysDictData.getDictLabel());
                    }
                }
            }
            if (phoneService != null) {
                for (SysDictData sysDictData : phoneService) {
                    String[] configService = productConfigVO.getConfigService().split(",");
                    for (String service : configService) {
                        if(Objects.equals(service, sysDictData.getDictValue())){
                            if(StringUtils.isNotBlank(configServiceName)){
                                configServiceName = configServiceName + ","+sysDictData.getDictLabel();
                            }else {
                                configServiceName = sysDictData.getDictLabel();
                            }
                        }
                    }
                }
            }
            productConfigVO.setConfigServiceName(configServiceName);
            resultList.add(productConfigVO);
        }
        return resultList;
}
    @Override
    public String add(CallProductConfigVO callProductConfigVO){
        CallProductConfig entity = new CallProductConfig();
        BeanUtils.copyProperties(callProductConfigVO, entity, SysConf.STATUS);
        entity.insert();
        callProductConfigVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);}
    @Override
    public String edit(CallProductConfigVO callProductConfigVO){
        CallProductConfig entity = callProductConfigService.getById(callProductConfigVO.getUid());
        BeanUtils.copyProperties(callProductConfigVO, entity);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}
    @Override
    public String delete(CallProductConfigVO callProductConfigVO){
        CallProductConfig entity = callProductConfigService.getById(callProductConfigVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}

    /**
     * 自动生成通话产品编码
     *
     * @return
     */
    @Override
    public String getAutoProductCode() {
        HttpServletRequest request = RequestHolder.getRequest();
        String ownerUid = null;
        Integer ownerType = null;
        if (request.getAttribute(SysConf.OWNER_UID) != null) {
            ownerUid = request.getAttribute(SysConf.OWNER_UID).toString();
            ownerType = (Integer) request.getAttribute(SysConf.OWNER_TYPE);
        }
        if (ownerType == null || ownerUid == null) {
            AdminVO adminVO = adminService.getMe();
            ownerType = adminVO.getOwnerType();
            ownerUid = adminVO.getCompanyUid();
        }
        //获取自动生成人员编号
        SequenceVO sequenceVO = new SequenceVO();
        sequenceVO.setOwnerType(ownerType);
        sequenceVO.setOwnerUid(ownerUid);
        sequenceVO.setBusiType(EBusiType.callProductCode);
        sequenceVO.setCodeType(EBusinessType.employeeAutoCode);
        Sequence sequence = sequenceService.customizeSelect(sequenceVO);
        String code = sequenceService.generateSerialCode(sequence);
        return code;
    }
}
